package com.httam.taxiSmart;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

/**
 * This class process basics for main menu of TaxiSmart app contains see taxi
 * list, cost first and cost GPS
 * 
 * @author Sparkle (' - ')
 * 
 */

public class MainActivity extends Activity implements OnClickListener {

	// Handle widgets UI
	private ImageButton btnListTaxi;
	private ImageButton btnCost;
	private ImageButton btnCostGPS;

	private Button btnSaigon;
	private Button btnSavico;
	private Button btnMaiLinh;
	private Button btnVinaSun;
	private Button btnHoangLong;
	private Button btnPetrolimex;
	
	public static final String pathSQLite = "/data/data/com.httam.taxiSmart/cache/Database_TaxiSmart.db";

	/** Initializing activity */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Map GUI to java code
		btnListTaxi = (ImageButton) findViewById(R.id.btnListTaxi);
		btnCost = (ImageButton) findViewById(R.id.btnCost);
		btnCostGPS = (ImageButton) findViewById(R.id.btnCostGPS);

		btnSaigon = (Button) findViewById(R.id.btnSaigon);
		btnSavico = (Button) findViewById(R.id.btnSavico);
		btnMaiLinh = (Button) findViewById(R.id.btnMaiLinh);
		btnVinaSun = (Button) findViewById(R.id.btnVinaSun);
		btnHoangLong = (Button) findViewById(R.id.btnHoangLong);
		btnPetrolimex = (Button) findViewById(R.id.btnPetro);

		// Set event listener
		btnListTaxi.setOnClickListener(this);
		btnCost.setOnClickListener(this);
		btnCostGPS.setOnClickListener(this);

		btnSaigon.setOnClickListener(this);
		btnSavico.setOnClickListener(this);
		btnMaiLinh.setOnClickListener(this);
		btnVinaSun.setOnClickListener(this);
		btnHoangLong.setOnClickListener(this);
		btnPetrolimex.setOnClickListener(this);

		// Create database
		File f = new File(getCacheDir() + "/Database_TaxiSmart.db");

		if (!f.exists()) {
			try {
				InputStream is = getAssets().open("Database_TaxiSmart.db");
				int size = is.available();
				byte[] buffer = new byte[size];
				is.read(buffer);
				is.close();

				FileOutputStream fos = new FileOutputStream(f);
				fos.write(buffer);
				fos.close();

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_option, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.help:
			HelpDialog helpDialog = new HelpDialog(MainActivity.this);
			helpDialog.setTitle("Hướng dẫn sử dụng");
			helpDialog.show();
			return true;
		case R.id.about:
			// Show About activity
			startActivity(new Intent(this, AboutActivity.class));
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// Show activity list taxi
	public void showListTaxi(View v) {
		// Create an Intent to start ListTaxiActivity
		Intent intent = new Intent(MainActivity.this, ListTaxiActivity.class);
		// Start the intent activity
		startActivity(intent);
	}

	// Show activity evaluate first cost
	private void showFirstCost(View v) {
		// Create an Intent to start FirstCostActivity
		Intent intent = new Intent(MainActivity.this, CostActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// Start the intent activity
		startActivity(intent);
	}

	private void showCostGPS(View v) {
		// Create an Intent to start LastCostActivity
		Intent intent = new Intent(MainActivity.this, GPSCostActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// Start the intent activity
		startActivity(intent);
	}

	// Handler event call taxis
	private void callSaiGonTaxi() {
		AlertDialog.Builder ald = new AlertDialog.Builder(this)
				.setMessage("Bạn có muốn gọi đến hãng SaiGon Taxi?")
				.setIcon(R.drawable.ic_launcher).setTitle(R.string.app_name);

		ald.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				CallIntent();
			}
		});

		ald.setNegativeButton("No", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		}).setCancelable(false).create();

		ald.show();
	}

	private void callSavicoTaxi() {
		AlertDialog.Builder ald = new AlertDialog.Builder(this)
				.setMessage("Bạn có muốn gọi đến hãng Savico Taxi?")
				.setIcon(R.drawable.ic_launcher).setTitle(R.string.app_name);

		ald.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				CallIntent();
			}
		});

		ald.setNegativeButton("No", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		}).setCancelable(false).create();

		ald.show();
	}

	private void callMainLinhTaxi() {
		AlertDialog.Builder ald = new AlertDialog.Builder(this)
				.setMessage("Bạn có muốn gọi đến hãng taxi Mai Linh?")
				.setIcon(R.drawable.ic_launcher).setTitle(R.string.app_name);

		ald.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				CallIntent();
			}
		});

		ald.setNegativeButton("No", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		}).setCancelable(false).create();

		ald.show();
	}

	private void CallIntent() {
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:123456"));
		startActivity(callIntent);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnListTaxi:
			showListTaxi(v);
			break;
		case R.id.btnCost:
			showFirstCost(v);
			break;
		case R.id.btnCostGPS:
			showCostGPS(v);
			break;
		case R.id.btnSaigon:
			callSaiGonTaxi();
			break;
		case R.id.btnSavico:
			callSavicoTaxi();
			break;
		case R.id.btnMaiLinh:
			callMainLinhTaxi();
			break;
		default:
			break;
		}
	}

}
